//
//  kata_DriveViewController.m
//  CityStar
//
//  Created by wangjia on 14-7-13.
//  Copyright (c) 2014年 kata. All rights reserved.
//

#import "kata_DriveViewController.h"
#import "kata_StopViewController.h"
#import "HTDefine.h"

@interface kata_DriveViewController (){
    UIPickerView *pickerView;
    NSArray *aryTime;
    
    CLLocationManager	* locationManager;
}

@end

@implementation kata_DriveViewController

- (id)initWithStyle:(UITableViewStyle)style
{
    self = [super initWithStyle:style];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.tableView.sectionHeaderHeight = 0;
    self.tableView.sectionFooterHeight = 0;
    self.tableView.backgroundColor = rgba(246, 245, 241, 1);
    self.title = @"代驾";
    
    UIButton * regBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 35, 44)];
    regBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [regBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [[regBtn titleLabel] setFont:[UIFont boldSystemFontOfSize:15]];
    [regBtn setTitle:@"信息" forState:UIControlStateNormal];
    [regBtn setTitleColor:[UIColor colorWithRed:0.991 green:0.532 blue:0.303 alpha:1.000] forState:UIControlStateHighlighted];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:regBtn];
    [regBtn addTarget:self action:@selector(infoAction) forControlEvents:UIControlEventTouchUpInside];
    
    pickerView = [[UIPickerView alloc]init];
    pickerView.delegate = self;
    
    self.tfTime.delegate = self;
    self.tfTime.inputView = pickerView;
    aryTime = @[@"07:00-22:00",@"22:00-23:00",@"23:00-00:00",@"00:00-07:00"];
    
    self.tfTime.text = [aryTime objectAtIndex:0];
    self.lblTile.text = self.tfTime.text;
    [self.tfAddress becomeFirstResponder];
    
    
    UIImageView *iv = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"larrow"] highlightedImage:[UIImage imageNamed:@"larrow_touch"]];
    iv.bounds = CGRectMake(0, 0, 8, 12);
    [self.cellTime setAccessoryView:iv];
    
    UIView *vBack = [[UIView alloc]initWithFrame:self.cellTime.bounds];
    
    vBack.backgroundColor = rgba(251, 183, 91, 1);
    
    self.cellTime.selectedBackgroundView = vBack;
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 90, 30)];
    UIButton *leftButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [leftButton setFrame:CGRectMake(-30, 0, 90, 30)];
    [leftButton setTitle:@"返回" forState:UIControlStateNormal];
    leftButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [leftButton setTitleColor:[UIColor colorWithWhite:0.200 alpha:1.000] forState:UIControlStateNormal];
    [leftButton setTitleColor:[UIColor colorWithRed:0.991 green:0.532 blue:0.303 alpha:1.000] forState:UIControlStateHighlighted];
    [leftButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    [leftButton setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    [leftButton setImage:[UIImage imageNamed:@"back_touch"] forState:UIControlStateHighlighted];
    
    [view addSubview:leftButton];
    
    UIBarButtonItem *barButtonItem = [[UIBarButtonItem alloc] initWithCustomView:view];
    self.navigationItem.leftBarButtonItem = barButtonItem;
    
    [self startToLoc];
}

-(void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 15;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    [self.tfTime becomeFirstResponder];
}

- (void)infoAction{
    [self performSegueWithIdentifier:@"segDriveInfo" sender:nil];
//    segDriveSelect
}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    return [aryTime objectAtIndex:row];
}

- (void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    [self.tfTime resignFirstResponder];
    self.tfTime.text = [aryTime objectAtIndex:row];
    self.lblTile.text = self.tfTime.text;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return aryTime.count;
}

- (IBAction)driveAction:(id)sender {
    [self performSegueWithIdentifier:@"segDriveSelect" sender:nil];
}

#pragma mark - 开始定位
- (void)startToLoc
{
	if (!locationManager) {
		locationManager = [[CLLocationManager alloc] init];
		locationManager.delegate = self;
		locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
		locationManager.distanceFilter = kCLDistanceFilterNone;
	}
	[locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
	[locationManager stopUpdatingLocation];
	
	CLGeocoder * geocoder = [[CLGeocoder alloc] init];
	
	[geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray * arr, NSError * err){
		if (arr.count > 0) {
			CLPlacemark * placeMark = [arr objectAtIndex:0];
        
			NSString * city = [@"" stringByAppendingFormat:@"%@%@%@%@",placeMark.locality,placeMark.subLocality,placeMark.thoroughfare,placeMark.subThoroughfare];
            
            city = [city stringByReplacingOccurrencesOfString:@"(null)" withString:@""];
            
            self.tfAddress.text = city;
		}
	}];
}


#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    if ([segue.identifier isEqualToString:@"segDriveSelect"]) {
        kata_StopViewController *mcDetailViewController = (kata_StopViewController *)segue.destinationViewController;
        
        mcDetailViewController.intID = 4217;
        
    }
}


- (void)viewDidUnload {
    [self setLblTile:nil];
    [self setCellTime:nil];
    [super viewDidUnload];
}
@end
